package com.cbianc.structure.adapter.example

//目标接口
internal interface TwoWayTarget {
    fun request()
}


//目标实现
internal class TargetRealize : TwoWayTarget {
    override fun request() {
        println("目标代码被调用！")
    }
}

//适配者接口
internal interface TwoWayAdaptee {
    fun specificRequest()
}
//适配者实现
internal class AdapteeRealize : TwoWayAdaptee {
    override fun specificRequest() {
        println("适配者代码被调用！")
    }
}

//双向适配器
internal class TwoWayAdapter : TwoWayTarget, TwoWayAdaptee {
    private var target: TwoWayTarget? = null
    private var adaptee: TwoWayAdaptee? = null

    constructor(target: TwoWayTarget) {
        this.target = target
    }

    constructor(adaptee: TwoWayAdaptee) {
        this.adaptee = adaptee
    }

    override fun request() {
        adaptee!!.specificRequest()
    }

    override fun specificRequest() {
        target!!.request()
    }
}

//客户端代码
object TwoWayAdapterTest {
    @JvmStatic
    fun main(args: Array<String>) {
        println("目标通过双向适配器访问适配者：")
        var adaptee: TwoWayAdaptee = AdapteeRealize()
        var target: TwoWayTarget = TwoWayAdapter(adaptee)
        target.request()
        println("-------------------")
        println("适配者通过双向适配器访问目标：")
        target = TargetRealize()
        adaptee = TwoWayAdapter(target)
        adaptee.specificRequest()
    }
}